Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add mock artifacts plugins #5741

Merged
merged 4 commits into from
Sep 17, 2024
Merged

add mock artifacts plugins #5741

merged 4 commits into from
Sep 17, 2024

Conversation

kanej
Copy link
Member

@kanej kanej commented Sep 12, 2024

While the build system is under development, this PR adds a temporary artifacts plugin that adds a fake artifacts manager to the hre under hre.artifacts.

This hre.artifacts will be replaced, but for the moment you can code against it by leveraging createMockHardhatRuntimeEnvironment in your tests. This will apply a mock artifacts plugin that you can use in your tests like so:

describe("createMockHardhatRuntimeEnvironment", () => {
  it("should allow plugins that leverage the artifact hre object", async () => {
    // arrange
    const exampleArtifact: Artifact = {
      _format: "hh-sol-artifact-1",
      contractName: "MyContract",
      sourceName: "source.sol",
      abi: [],
      bytecode: "0x",
      linkReferences: {},
      deployedBytecode: "0x",
      deployedLinkReferences: {},
    };

    const myPlugin: HardhatPlugin = {
      id: "my-plugin",
      tasks: [
        task("hello-artifact-using-world", "Tests artifact loading")
          .setAction(async ({}, hre: HardhatRuntimeEnvironment) => {
            return hre.artifacts.readArtifact("MyContract");
          })
          .build(),
      ],
    };

    const mockHre = await createMockHardhatRuntimeEnvironment({
      plugins: [myPlugin],
    });

    await mockHre.artifacts.saveArtifact(exampleArtifact);

    // act
    const helloArtifactUsingWorld = mockHre.tasks.getTask(
      "hello-artifact-using-world",
    );

    const result = await helloArtifactUsingWorld.run({});

    // Assert
    assert.equal(result, exampleArtifact);
  });
});

Copy link

changeset-bot bot commented Sep 12, 2024

⚠️ No Changeset found

Latest commit: 9fc2bd0

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link

vercel bot commented Sep 12, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
hardhat ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 17, 2024 9:29am

@github-actions github-actions bot added the status:ready This issue is ready to be worked on label Sep 12, 2024
@kanej kanej marked this pull request as draft September 12, 2024 13:37
Copy link
Contributor

hardhat

Total size of the bundle: 79M
Total number of dependencies (including transitive): 119

List of dependencies (sorted by size)
77M	total
23M	solc
20M	esbuild
4.9M	lodash
2.9M	fp-ts
2.8M	@sentry/tracing
2.1M	secp256k1
1.9M	@noble/curves
1.6M	undici
1.2M	@sentry/types
1.2M	@noble/hashes
1.1M	@ignored/hardhat-vnext-build-system
996K	@nomicfoundation/ethereumjs-util
952K	node-addon-api
932K	@sentry/node
920K	@sentry/utils
892K	keccak
824K	zod
712K	@ignored/hardhat-vnext-utils
576K	tsx
556K	resolve
548K	@sentry/core
504K	fast-equals
492K	@scure/bip39
368K	ethereum-cryptography
344K	@sentry/hub
320K	enquirer
284K	semver
268K	fs-extra
248K	scrypt-js
244K	io-ts
200K	undici-types
196K	readable-stream
192K	@nomicfoundation/ethereumjs-rlp
180K	blakejs
176K	elliptic
168K	@scure/base
164K	@ignored/hardhat-vnext-errors
136K	adm-zip
128K	get-tsconfig
112K	bn.js
104K	hash.js
96K	commander
96K	browserify-aes
96K	@scure/bip32
92K	chalk
88K	tslib
88K	@sentry/minimal
84K	js-sha3
76K	agent-base
72K	sha.js
72K	@nomicfoundation/solidity-analyzer
68K	glob
68K	debug
64K	lru_map
64K	https-proxy-agent
56K	rfdc
56K	graceful-fs
56K	follow-redirects
52K	pbkdf2
52K	hmac-drbg
48K	safe-buffer
48K	minimatch
48K	memorystream
48K	command-exists
48K	ansi-colors
48K	@ignored/hardhat-vnext-zod-utils
44K	tmp
44K	node-gyp-build
40K	resolve-pkg-maps
40K	buffer-xor
36K	klaw
36K	concat-map
32K	string_decoder
32K	jsonfile
32K	fs.realpath
32K	create-hash
32K	cookie
28K	util-deprecate
28K	ripemd160
28K	randombytes
28K	minimalistic-crypto-utils
28K	create-hmac
28K	base-x
24K	strip-ansi
24K	p-map
24K	md5.js
24K	inherits
24K	indent-string
24K	env-paths
24K	clean-stack
24K	cipher-base
24K	bs58check
24K	brorand
24K	brace-expansion
24K	ansi-regex
24K	aggregate-error
24K	@types/secp256k1
20K	wrappy
20K	universalify
20K	setimmediate
20K	require-from-string
20K	path-parse
20K	path-is-absolute
20K	path-exists
20K	p-try
20K	p-locate
20K	p-limit
20K	os-tmpdir
20K	once
20K	ms
20K	minimalistic-assert
20K	locate-path
20K	inflight
20K	hash-base
20K	find-up
20K	evp_bytestokey
20K	bs58
20K	balanced-match
20K	@types/pbkdf2

Add an in-built plugin that `extends` the HRE with an `artifacts`
implementation based on the v2 interface.
We add a `createMockHardhatRuntimeEnvironment` that overrides the
standard artifacts built-in plugin and substitutes in a mock version
that can be leveraged in tests and development.

There is a supporting test to show the usage of the mocked artifacts
manager.
By making not implemented an explicit error, we can track it in the code
base (which is hardher if we piggy back on another error type).
@kanej kanej marked this pull request as ready for review September 16, 2024 21:34
Copy link
Member

@alcuadrado alcuadrado left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left a minor comment, but looks good to me

Match the pattern used for the network manager loading.

This is the only category of hook where we lazily load as it is loaded
every time the HRE is setup.
@kanej kanej added this pull request to the merge queue Sep 17, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Sep 17, 2024
@kanej kanej added this pull request to the merge queue Sep 17, 2024
Merged via the queue into v-next with commit 198612d Sep 17, 2024
54 checks passed
@kanej kanej deleted the feat/hre-artifacts branch September 17, 2024 10:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:ready This issue is ready to be worked on v-next A Hardhat v3 development task
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants